//
//  BQTabBarController.m
//  BeeQuick
//
//  Created by Asun on 2017/7/9.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQTabBarController.h"
#import "BQNaigationViewController.h"
#import "UIColor+MLAddition.h"
#import "BQShopTableViewController.h"
@interface BQTabBarController () <UITabBarControllerDelegate>

@end

@implementation BQTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray * mustableArray = [NSMutableArray array];
    
    [mustableArray addObject:[self addChildControllerWith:@"BQHomeTableViewController" title:@"首页" normalImage:@"v2_home"]];
    
    [mustableArray addObject:[self addChildControllerWith:@"BQListViewController" title:@"闪送超市" normalImage:@"v2_order"]];
    [mustableArray addObject:[self addChildControllerWith:@"BQShopTableViewController" title:@"购物车" normalImage:@"shopCart"]];
    [mustableArray addObject:[self addChildControllerWith:@"BQMineTableViewController" title:@"我的" normalImage:@"v2_my"]];
    
    self.viewControllers = mustableArray;
    
    [self setupTabBarAnimation];
    
    self.delegate = self;
}


#pragma mark - 封装TabBar -
- (BQNaigationViewController *)addChildControllerWith:(NSString *)clsName title:(NSString *)title normalImage:(NSString *)normalImage {
    
    // 1. 转换class
    Class cls = NSClassFromString(clsName);
    
    // 2. 实例化控制器
    UIViewController *controller = [[cls alloc] init];
    
    // 3. 设置控制器属性
    
    // title
    controller.title = title;
    
    // 设置图片
    controller.tabBarItem.image = [UIImage imageNamed:normalImage];
    
    // 设置选中的图片
    NSString *selImage = [normalImage stringByAppendingString:@"_selected"];
    
    UIImage *image = [[UIImage imageNamed:selImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    controller.tabBarItem.selectedImage = image;
    
    // 设置tabbar上字体的颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateSelected];
    
    // 4. 实例化导航控制器
    BQNaigationViewController *nav = [[BQNaigationViewController alloc] initWithRootViewController:controller];
    
    return nav;
}

#pragma mark -  点击tabbarItem动画效果 -
- (void)setupTabBarAnimation{
    for (UIControl *tabBarButton in self.tabBar.subviews)
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
}

// 动画效果
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            // 需要实现的帧动画,这里根据需求自定义
            CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            pulse.duration = 0.15;
            pulse.repeatCount = 1;
            pulse.autoreverses = YES;
            pulse.fromValue = [NSNumber numberWithFloat:0.6];
            pulse.toValue = [NSNumber numberWithFloat:1.4];

            [imageView.layer addAnimation:pulse forKey:nil];
        }
    }
}


-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    
    BQNaigationViewController *temp = (BQNaigationViewController *)viewController;

    
    if ([[temp.viewControllers objectAtIndex:0] isKindOfClass:[BQShopTableViewController class]]) {
        
        NSLog(@"%ld",temp.viewControllers.count);
        
        BQShopTableViewController *shop = [[BQShopTableViewController alloc] init];
        
        BQNaigationViewController *nav = [[BQNaigationViewController alloc]initWithRootViewController:shop];
        
        [self presentViewController:nav animated:YES completion:nil];
        
        return NO;
    }
    
    return YES;
    
}

@end
